//
//  ViewController.m
//  22--AFNetWorking
//
//  Created by 冀永金 on 16/8/12.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //[self netWorkingGet];
    [self netPost];
    // Do any additional setup after loading the view, typically from a nib.
   }
#pragma mark-----Post 请求
- (void)netPost {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *postURLString = @"http://open3.bantangapp.com/base/app/init";
    NSString *parmeters = @"app_id=com.jzyd.BanTang&app_installtime=1467276561&app_versions=5.8.5&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&device_token=1aa2741a8e58dbf700d96e588b551a5bf7f8a1e222eb9948c836ba717ce0d48e&os_versions=10.0&screensize=750&track_device_info=iPhone8%2C1&track_deviceid=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&v=17";
    // 设置请求参数的字符串格式  自定义
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return parmeters;
    }];
    [manager POST:postURLString parameters:parmeters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",task.currentRequest.HTTPBody);
        NSLog(@"成功：%@",responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败：%@",error);
    }];
}


#pragma mark-------Get请求
- (void)netWorkingGet {
    AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];
    NSString *requestURLString = @"http://cars.app.autohome.com.cn/cars_v5.8.0/cars/getmarks-a2-pm1.json";
    // 1.requestSeriallizer 格式化Body / 参数
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    
    // 设置请求头，设置responseSerializer中的字典
    [manager.requestSerializer setValue:@"" forHTTPHeaderField:@""];
    
    // 默认60
    manager.requestSerializer.timeoutInterval = 90;
    // 返回数据进行格式化
    //responseSerializer 默认类型AFJSONResponseSerializer
    // 保留系统中的ContentType,添加新的Type
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
    
    // urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [manager GET:requestURLString parameters:@{@"name":@"wanghuang"} progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",task.currentRequest.URL);
        NSLog(@"请求成功:%@", [responseObject class]);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",task.currentRequest.URL);
        NSLog(@"------------请求失败:%@", error);
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
